//
// Created by REEJI2219 on 2024-08-22.
//
#include <iostream>
#include <stdarg.h>
using namespace std;
typedef int ElemType;
class Array {
private:
    //单个维度存储数据
    int *base;
    //维度
    int dim;
    int *bounds;//存储维度结构
    int *constants;//函数组映射函数
    int size;
public:
    Array() {
        base=nullptr;
        dim=0;
        size=0;
        bounds=nullptr;
        constants=nullptr;
    }
    ~Array() {
        delete[] base;
        delete[] bounds;
        delete[] constants;
    }
    bool initArray(int dim,...) {
        if(dim<=0) {
            return false;
        }
        this->dim = dim;
        bounds = new int[dim];
        va_list args;
        va_start(args, dim);
        int count = 1;
        for (int i=0;i<dim;i++) {
            bounds[i]=va_arg(args, int);
            count=count*bounds[i];
        }
        base = new int[count];
        this->size=count;
        constants = new int[dim];
        constants[dim-1]=1;
        for(int i=dim-2;i>=0;i--) {
            constants[i]=bounds[i]*constants[i+1];
        }
        va_end(args);
        return true;
    }
    void print() {
        printf("数组长度%d 维度%d \n",this->size,this->dim);
        for (int i=0;i<dim;i++) {
            printf("%d ",bounds[i]);
        }
        printf("\n");
        for (int i=0;i<dim;i++) {
            printf("%d ",constants[i]);
        }
        printf("\n");
    }
    void add(int value,...) {
        va_list args;
        va_start(args, value);
        int index = 0;
        for (int i=0;i<dim;i++) {
            index = index+constants[i]+va_arg(args, int);
        }
        base[index] = value;
    }
    bool get(int *result,...) {
        va_list args;
        va_start(args,result);
        int index = 0;
        for (int i=0;i<dim;i++) {
            index = index+constants[i]+va_arg(args, int);
        }
        *result = base[index];
    }
};

// int main(int argc, char *argv[]) {
//     Array *array = new Array();
//     array->initArray(3,3,2,3);
//     array->print();
//     array->add(100,1,1,1);
//     int data = 0;
//     array->get(&data,1,1,1);
//     cout<<data<<endl;
// }
